package com.example.springboot.dao;
import com.example.springboot.model.admin_menu;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface admin_menuMapper {
    //查询全部 可以不用担心分页为什么会查询全部  PageHelper.startPage会拦截下一个sql，也就是mybatisDao.findAll()的SQL。并且根据当前数据库的语法，把这个SQL改造成一个高性能的分页SQL，同时还会查询该表的总行数，具体可以看SQL日志
    @Select("select * from admin_menu")
    List<admin_menu> findAll();
    //添加新项目
    @Insert({"insert into admin_menu (name,url,icon,addtime,sort,parentid,remarks)values (#{name},#{url},#{icon},#{addtime},#{sort},#{parentid},#{remarks})"})
    public int save(admin_menu admin_menu);
    //根据id删除
    @Delete(" delete from admin_menu where id= #{id} ")
    public void delete(int id);
    //根据id查询某一项
    @Select("select * from admin_menu where id= #{id} ")
    public admin_menu get(int id);
    //通用更新
    @Update("update admin_menu set name=#{name}，url=#{url},icon=#{icon},addtime=#{addtime},sort=#{sort},parentid=#{parentid},remarks=#{remarks} where id=#{id} ")
    public int update(admin_menu admin_menu);

    //结构化sql
    @SelectProvider(type = UserDaoProvider.class, method = "findUserByName")
    List<admin_menu> FindAllWhere(Map<String, Object> map);

    class UserDaoProvider {
        public String findUserByName(Map<String, Object> map) {
            String sql = "SELECT * FROM admin_menu";
            Integer type=Integer.parseInt(map.get("type").toString()) ;
            String name=map.get("name")!=null?map.get("name").toString():null;
            if(type!=-1||name!=null){
                sql += " where ";
                int i=1;
                if(type!=-1){
                    sql += i!=1?"and ":"";
                    sql +="parentid !=0";//测试
                    i++;
                }
                if(name!=null){
                    sql += i!=1?"and ":"";
                    sql +="name LIKE  '%"+name+"%'";
                i++;
                }
            }

            return sql;
        }
    }
}